package com.jinkeen.xiaxie.chatRoom

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.jinkeen.xiaxie.R
import com.jinkeen.xiaxie.base.BaseActivity
import com.jinkeen.xiaxie.databinding.ActivityChatRoomBinding

class ChatRoomActivity : BaseActivity(){
    private var chatModels:ArrayList<ChatModel>?=null
    private lateinit var adapter: ChatRoomAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding=ActivityChatRoomBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val chatModel=ChatModel(0,1,123,"小李","我们已经是朋友了","receive")
        val chatModel2=ChatModel(0,1,123,"小吴","你好","send")

        chatModels= ArrayList()
        chatModels!!.add(chatModel)
        chatModels!!.add(chatModel2)
        adapter=ChatRoomAdapter(this,chatModels!!)
        adapter.notifyDataSetChanged()
        val layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        binding.rvChatRoom.layoutManager=layoutManager
        binding.rvChatRoom.adapter=adapter
        binding.top.imgBack.setOnClickListener {
            finish()
        }
        binding.top.title.text="小李"
        binding.top.tvRight.setOnClickListener {
            Toast.makeText(this, "还在开发中", Toast.LENGTH_SHORT).show()
        }


        binding.btnSend.setOnClickListener {
            chatModels!!.add(ChatModel(0,1,123,"小吴",binding.etContent.text.toString(),"send"))
            // TODO: 2021/5/26
            when(binding.etContent.text.toString()){
                ""->{}
                "你好"->{
                    chatModels!!.add(ChatModel(0,1,123,"小李","你好呀","receive"))
                }
                "你叫什么"->{
                    chatModels!!.add(ChatModel(0,1,123,"小李","我没叫呀","receive"))
                }
            }
            binding.etContent.text.clear()
            adapter.notifyDataSetChanged()
            binding.rvChatRoom.scrollToPosition(chatModels!!.size)
        }

    }
}